package org.coopac.dao.impl;

import java.util.List;

import javax.persistence.NoResultException;

import org.coopac.dao.IDocumentoPersonaDAO;
import org.coopac.model.CoopacDocumentoPersona;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("DocumentoPersonaDAO")
public class DocumentoPersonaDAO extends BaseDaoImpl<CoopacDocumentoPersona> implements IDocumentoPersonaDAO {
	
	@Autowired
	SessionFactory sessionFactory;
	
	@Override
	protected SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Override
	public List<CoopacDocumentoPersona> listar() {
		return super.listar(CoopacDocumentoPersona.class);
	}

	@Override
	public void save(CoopacDocumentoPersona documentoPersona) {
		super.save(documentoPersona);
	}

	@Override
	public void update(CoopacDocumentoPersona documentoPersona) {
		super.update(documentoPersona);
	}

	@Override
	public void delete(CoopacDocumentoPersona documentoPersona) {
		super.delete(documentoPersona);
	}

	@Override
	public CoopacDocumentoPersona getById(int id) {
		return (CoopacDocumentoPersona) super.getById(CoopacDocumentoPersona.class, id);
	}
	@Override
	public CoopacDocumentoPersona getPersonaByDocumento(String numero){
		@SuppressWarnings("rawtypes")
		List list = getSessionFactory().getCurrentSession()
		.createQuery("from CoopacDocumentoPersona where numero =? ").setParameter(0, numero).list(); 
		if(!list.isEmpty() && list!=null){
			
		}else{
			return null;
		}
		
		return (CoopacDocumentoPersona)list.get(0);
		        
	}

	@Override
	public CoopacDocumentoPersona getByIdPersona(Integer id) {
		@SuppressWarnings("rawtypes")
		List list = getSessionFactory().getCurrentSession()
					.createQuery("from CoopacDocumentoPersona s where  s.coopacPersona.idPersona =? ").setParameter(0, id).list(); 
		return (CoopacDocumentoPersona) list.get(0);
	}

}

